<!DOCTYPE HTML>
<html>
<link href="main.css" rel="stylesheet" type="text/css">
<title>SoftPlus</title>
<body>
<table border="0" align="center">
  <tr>
    <td width="150px"></td>
    <td width="1000px"><img src="banner-01n.gif" width="1000" height="90" alt="Softplus"></td>
  </tr>
  <tr>
    <td valign="top"><?php
      require("navandset.php");
    ?></td>
<td valign="top">

<h1>Reorders</h1>

<?php
//Connect
$condet = GetGlobalConnectionOptions();
$con = mysql_connect($condet["server"],$condet["username"],$condet["password"]);
if (!$con)
{
	die("Could not connect: " . mysql_error());
}
//Select DB
mysql_select_db($condet["database"], $con);

//Code
//Query to find the sales totals
$qry = "
SELECT tblreorders.reorder, tblreorders.creationdate, tblsuppliers.name
FROM tblreorders, tblsuppliers
WHERE tblsuppliers.supplier = tblreorders.supplier
AND creationdate BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()
ORDER BY creationdate DESC;";
$reorders = mysql_query($qry);
$numreorders = mysql_num_rows($reorders);
$reorderitems;
$reorderdet;
$reordertotal;

if($_GET)
{
	$qry = "SELECT tblreorders.reorder, tblreorders.creationdate, tblsuppliers.name, tblsuppliers.email
		FROM tblreorders, tblsuppliers
		WHERE tblreorders.reorder = '".$_GET["reorder"]."'
		AND tblsuppliers.supplier = tblreorders.supplier;";
	$reorderdet = mysql_fetch_array(mysql_query($qry));
	$qry = "SELECT tblreorderitems.item, tblreorderitems.quantity, tblitems.name, tblitems.wholesaleprice, tblreorderitems.quantity*tblitems.wholesaleprice AS total
		FROM tblreorderitems, tblitems
		WHERE tblreorderitems.item = tblitems.item
		AND tblreorderitems.reorder = '".$_GET["reorder"]."'
		ORDER BY tblitems.name;";
	$reorderitems = mysql_query($qry);
	$qry = "SELECT SUM(tblreorderitems.quantity*tblitems.wholesaleprice) AS total
		FROM tblreorderitems, tblitems
		WHERE tblreorderitems.item = tblitems.item
		AND tblreorderitems.reorder = '".$_GET["reorder"]."';";
	$reordertotal = mysql_fetch_array(mysql_query($qry));
};
?>

<form action="rptreorderemail.php" method="get">
	<h2>Reorder:
	<select name="reorder">
	<?php
		if($numreorders > 0)
		{
			while($row = mysql_fetch_array($reorders))
			{
				if($row["reorder"]==$_GET["reorder"])
				{
					echo "<option value='".$row["reorder"]."' selected='selected'>".$row["creationdate"].", ".$row["name"]."</option>";
				}
				else
				{
					echo "<option value='".$row["reorder"]."'>".$row["creationdate"].", ".$row["name"]."</option>";
				};
			};
		};
	?>
	</select>
	<input type="submit" name="operation" value="View Reorder"></h2>
</form>
<?php
	if($_GET)
	{
		//Title
		echo "<b>SoftPlus Pty Ltd</b></br>";
		//Our details
		echo "WELLINGON POINT, QLD, 4160</br>
		      Building 27, 38 Baar St</br>
		      Phone: 041 763 32 32</br>
		      Email: softplus@gmail.com</br></br>";
		//Reorder details
		echo "Order #";
		echo $reorderdet["reorder"];
		echo ", Date: ";
		echo $reorderdet["creationdate"];
		echo "</br></br>";
		//To (supplier details)
		echo "To:</br>";
		echo $reorderdet["name"];
		echo "</br>";
		echo $reorderdet["email"];
		echo "</br></br>";
		//Order (item list etc)
		echo "<table width='840px' border='1' cellpadding='0' cellspacing='0'>
		<tr>
		<th><b>Barcode</b></th>
		<th><b>Name</b></th>
		<th><b>Price per unit</b></th>
		<th><b>Quantity</b></th>
		<th><b>Total</b></th>
		</tr>";
		while($item = mysql_fetch_array($reorderitems))
		{
			echo "<tr>";
			echo "<td align='middle'>".$item["item"]."</td>";
			echo "<td align='middle'>".$item["name"]."</td>";
			echo "<td align='right'>$".$item["wholesaleprice"]."</td>";
			echo "<td align='middle'>".$item["quantity"]."</td>";
			echo "<td align='right'>$".$item["total"]."</td>";
			echo "</tr>";
		};
		//Total
		echo "<tr><td></td><td></td><td></td><td align='middle'><b>Total</b></td><td align='right'>$".$reordertotal["total"]."</td></tr>";
		echo "</table>";
	};
//Disconnect
mysql_close($con);
?>

</td></tr></table>
</body>
</html>
